package ecom.cart;

import ecom.entity.Product;
import java.util.*;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

/**
 *
 * @author simon
 */
@Stateless
@Remote(CartLocal.class)
public class CartBean implements CartLocal {
    
    List<Cart> cartList;
    
    public CartBean () {
//        this.cartMap = new HashMap<Integer, Cart>();
        this.cartList = new ArrayList<Cart>();
    }

    @Override
    public int createCart(Cart cart) {
        // recuperer la dernier clee
        int lastIndex = cartList.size();        
        cartList.add(cart);
        return cartList.size();
    }
    
    public Cart getCart(Integer cartId) {
        return this.cartList.get(cartId - 1);
    }

    
    public List<CartItem> getItems(Integer cartId) {
        return cartList.get(cartId - 1).getItems();
    }

    @Override
    public int getNumberOfItems(Integer cartId) {
        return cartList.get(cartId - 1).getNumberOfItems();
    }

    @Override
    public double getSubtotal(Integer cartId) {
        return cartList.get(cartId - 1).getSubtotal();
    }

    @Override
    public void addItem(Integer cartId, Product product, double price, int storeId) {
        cartList.get(cartId - 1).addItem(product, price, storeId);
    }

    @Override
    public void update(Integer cartId, Product product, String storeId, String quantity) {
        cartList.get(cartId - 1).update(product, storeId, quantity);
    }

    @Override
    public void calculateTotal(Integer cartId, String surcharge) {
        cartList.get(cartId - 1).calculateTotal(surcharge);
    }

    @Override
    public void clear(Integer cartId) {
        // Suppression du cart
        cartList.remove(cartId - 1);
    }

    @Override
    public double getTotal(Integer cartId) {
        return cartList.get(cartId - 1).getTotal();
    } 
}
